-
Notifications
You must be signed in to change notification settings - Fork 338
add mode for gas optimization #63
base: master
Are you sure you want to change the base?
Conversation
Good work! This is so useful. I wanted to suggest a simpler alternative that in the vast majority of situations saves more gas for a typical user. The usecase of the 4 leading zero-bytes is to be able to cast an address to a uint128 rather than a uint160. Specifically, if you have 2 of these addresses with 4 leading zero-bytes, you can pack both of them into a single uint256 storage slot. You can take advantage of this in your own custom contracts to parse each address from the front/back of the storage slot and prepend the 8 zeroes afterwards. If you plan on using both addresses frequently in your code, this saves a cool ~2000 gas during runtime in solidity. However, this usecase is extremely niche, and never used in the wild such that a typical user could take advantage. A more generalized --gas option would be for finding the highest total of zero-bytes within the address. This takes advantage of the much more applicable Gtxdatazero, which saves 12 gas compared to every non-zero byte in the address.
This allows you to find addresses with more zero-bytes than the more restricting condition that 4 zero-bytes must lead.
|
You are correct of course! A wallet user only cares about the number of zero bytes probably. I was making contracts, so this worked better for my needs at the time. |
Can you elaborate a bit more on the making contracts? Because doing a contract with less bytes also utilizes less gas too? I'm only asking because If theres something more useful that i'm not seeing here I would love the insight or at least a point in the direction that your utilizing it for. |
If someone wants/needs to customize for special contract circumstances such as proxies, they can do that themselves with minor tweaks here.
To @plotchy's point, a more generic gas optimization is better for most cases, so I simplified it. Leading/trailing/contiguous zero-bytes is generally better for a contract because you can pack it in less bytecode. Think of clone/proxy contracts in particular. ...but... the results took much longer when trying to get to something like that. I found the generic version was faster and suffices just fine for most cases. Even on my M1 Mac it performs well:
Got me:
(granted, this was a bit lucky... still really fantastic!) |
https://eip2535diamonds.substack.com/p/introduction-to-the-diamond-standard and utilizing above references seems fun. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forgot the closing brackets!
Based on #57